package com.aube.mis.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.springframework.stereotype.Service;

import com.aube.apiv2.service.app.extra.AppExtraVoService;
import com.aube.apiv2.vo.app.extra.AppExtraOptionRespVo;
import com.aube.apiv2.vo.app.extra.AppExtraOptionVo;
import com.aube.apiv2.vo.app.extra.AppExtraVo;
import com.aube.mis.dto.ExtraConfigDTO;
import com.aube.mis.dto.ExtraConfigOptionDTO;
import com.aube.mis.exception.MisException;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ExtraConfigService;
import com.aube.support.ResultCode;

@Service
public class ExtraConfigServiceImpl extends AbstractService implements ExtraConfigService {
	@Resource
	AppExtraVoService appExtraVoService;

	@Override
	public ExtraConfigDTO getExtraConfig(String showId, String relatedType, String relatedId) {
		try {
			ResultCode<AppExtraVo> vo = appExtraVoService.findRelateAppExtraVo(Long.parseLong(showId), relatedType, relatedId);
			List<ExtraConfigOptionDTO> extraConfigOptionList = getExtraConfigOptionList(relatedId, vo.getRetval().getId().toString());
			ExtraConfigDTO configDTO = convertExtraConfigDTO(vo.getRetval());
			configDTO.setOptionList(extraConfigOptionList);
			return configDTO;
		} catch (MisException e) {
			if(e.getCode().equals("9901")) {
				return null;
			} else {
				throw e;
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ExtraConfigDTO> getExtraConfigList(String showId, String relatedType) {
		ResultCode<List<AppExtraVo>> voList = appExtraVoService.findAppExtraVoListByRelateType(Long.parseLong(showId), relatedType);
		List<ExtraConfigDTO> result = (List<ExtraConfigDTO>) CollectionUtils.collect(voList.getRetval(), new Transformer() {

			@Override
			public ExtraConfigDTO transform(Object input) {
				AppExtraVo vo = (AppExtraVo) input;
				return convertExtraConfigDTO(vo);
			}
		});
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExtraConfigOptionDTO> getExtraConfigOptionList(String relatedId, String configId) {
		ResultCode<List<AppExtraOptionRespVo>> voList = appExtraVoService.findAllOptionsByExtraId(Long.parseLong(configId), relatedId);
		List<ExtraConfigOptionDTO> result = (List<ExtraConfigOptionDTO>) CollectionUtils.collect(voList.getRetval(), new Transformer() {

			@Override
			public ExtraConfigOptionDTO transform(Object input) {
				AppExtraOptionRespVo vo = (AppExtraOptionRespVo) input;
				return convertExtraConfigOptionDTO(vo);
			}
		});
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void saveExtraConfig(String showId, String relatedId, ExtraConfigDTO extraConfigDTO) {
		List<AppExtraOptionVo> optionList = (List<AppExtraOptionVo>) CollectionUtils.collect(extraConfigDTO.getOptionList(), new Transformer() {

			@Override
			public AppExtraOptionVo transform(Object input) {
				ExtraConfigOptionDTO dto = (ExtraConfigOptionDTO) input;
				AppExtraOptionVo result = new AppExtraOptionVo();
				result.setOptionId(Long.parseLong(dto.getOptionId()));
				result.setOptionValue(dto.getOptionValue());
				return result;
			}
		});
		appExtraVoService.saveExtraOptions(Long.parseLong(showId), Long.parseLong(extraConfigDTO.getConfigId()), relatedId, optionList);
	}
	
	private ExtraConfigDTO convertExtraConfigDTO(AppExtraVo vo) {
		ExtraConfigDTO result = new ExtraConfigDTO();
		result.setConfigId(vo.getId().toString());
		result.setConfigCode(vo.getConfigCode());
		result.setConfigName(vo.getConfigName());
		result.setConfigDesc(vo.getConfigDesc());
		return result;
	}

	private ExtraConfigOptionDTO convertExtraConfigOptionDTO(AppExtraOptionRespVo vo) {
		ExtraConfigOptionDTO result = new ExtraConfigOptionDTO();
		result.setOptionId(vo.getOptionId().toString());
		result.setOptionValue(vo.getOptionValue());
		result.setValueType(vo.getValueType());
		result.setOptionName(vo.getOptionName());
		return result;
	}

}
